package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletCardsAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCardsContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.FamilyCard;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletCardsPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletFamilyCardsActivity extends BaseToolbarActivity implements WalletCardsContract.View {
    private TextView mTvReality;
    private TextView mTvVirtual;
    private RecyclerView mRv;
    private LinearLayout mLlNone;
    private WalletCardsAdapter mAdapter;
    @Inject
    WalletCardsPresenter mPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);

        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_family_card));
        resisterPush();
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_cards;
    }

    /**
     * 初始化Id
     */
    private void initId() {
        mTvReality = getViewById(R.id.tv_wallet_cards_add_reality);
        mTvVirtual = getViewById(R.id.tv_wallet_cards_add_virtual);
        mRv = getViewById(R.id.rv_wallet_cards);
        mLlNone = getViewById(R.id.ll_wallet_cards_none);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
        RxUtils.bindEvents(mTvReality, this);
        RxUtils.bindEvents(mTvVirtual, this);
    }


    private void resisterPush() {
        Disposable subscribe = RxBus.get().toFlowable(EventBean.class).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null && bean.type == Constants.EVENT_TYPE_FAMILY_CARD_UPDATE) {
                    initInfo();
                }
            }
        });
        mCompositeDisposable.add(subscribe);
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        getCards();
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.tv_wallet_cards_add_reality:
                Intent intent = new Intent(this, WalletScanCardActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_wallet_cards_add_virtual:
                Intent i = new Intent(this, WalletAddCardActivity.class);
                startActivity(i);
                break;
        }
    }

    @Override
    protected void onRefreshData() {
        if (mAdapter != null)
            if (mAdapter.isLoading())
                mAdapter.loadMoreEnd();
        getCards();
    }


    @Override
    public void showError(String msg) {

    }


    private void getCards() {
        mPresenter.getCards();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        refreshFinish();
    }

    @Override
    public void getCardsBack(List<FamilyCard> cards, String msg) {
        refreshFinish();
        if (msg != null) {
            mLlNone.setVisibility(View.VISIBLE);
            return;
        }
        if (mAdapter == null) {
            mAdapter = new WalletCardsAdapter(this, cards);
            mRv.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    FamilyCard item = (FamilyCard) adapter.getItem(position);
                    Intent intent = new Intent(WalletFamilyCardsActivity.this,
                            WalletCardDetailActivity.class);
                    intent.putExtra(Constants.ARGUMENT, item);
                    startActivity(intent);
                }
            });
        } else
            mAdapter.replaceData(cards);
        mLlNone.setVisibility(cards.isEmpty() ? View.VISIBLE : View.GONE);
    }
}
